package com.baiyun.yunpan.controller;

import com.baiyun.yunpan.Util.MinioUtils;
import com.baiyun.yunpan.Util.R;
import com.baiyun.yunpan.entity.FilesDetail;
import io.minio.MinioClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController

public class FileController {

    @GetMapping("/file/getuserfile")
    @CrossOrigin("*")
    public R getUserFile(String userName, String prefix){
        try{

            MinioUtils minio = MinioUtils.getMinio();
            List<FilesDetail> objects = minio.getObjects(userName, prefix);
            return R.ok().put("data", objects);
        }catch(Exception e){
            return R.err().put("data", e.getMessage());
        }
    }

    @GetMapping("/file/getalluserfile")
    @CrossOrigin("*")
    public R getUserFile(String userName){
        try{

            MinioUtils minio = MinioUtils.getMinio();
            List<FilesDetail> objects = minio.getAllObjects(userName);
            return R.ok().put("data", objects);
        }catch(Exception e){
            return R.err().put("data", e.getMessage());
        }
    }

    /***
     * 文件上传
     * @param files
     * @param bucketName
     * @param path
     * @return
     */

    @PostMapping("/files/objectupload")
    public R objectUpload(@RequestParam("file") MultipartFile files, String bucketName, String path){
        try{
            String originalFilename = files.getOriginalFilename();
            System.out.println(originalFilename);
            String pathName=path+originalFilename;
            InputStream inputStream = files.getInputStream();
            MinioUtils minio = MinioUtils.getMinio();
            System.out.println("123");

            minio.objectUpload(pathName,inputStream,bucketName,files.getContentType());
            return R.ok().put("msg", "上传成功");
        }catch(Exception e){
            return R.err().put("msg", e.getMessage());
        }


    }

    /***
     * 删除文件
     * @param bucketName
     * @param objectNames
     * @return
     */
    @GetMapping("/files/objectdelete")
    public R objectDetele(String bucketName,String objectNames){
        try{
            if(bucketName==null || objectNames==null){
                throw new RuntimeException("参数非法");
            }
            MinioUtils minio = MinioUtils.getMinio();
            String[] objectName = objectNames.split(",");
            for (int i=0;i<objectName.length;i++){
                minio.deleteObjetc(bucketName, objectName[i]);
            }
            return R.ok().put("msg", "删除成功");

        }catch(Exception e){
            return R.err().put("msg", e.getMessage());
        }

    }

    /**
     * 删除bucket
     * @param bucketNames
     * @return
     */
    @GetMapping("/files/bucketdelete")
    public R deleteBucket(String bucketNames){
        try{
            MinioUtils minio = MinioUtils.getMinio();
            String[] bucketName = bucketNames.split(",");
            for (int i=0;i<bucketName.length;i++){
                minio.deleteBucket(bucketName[i]);
            }
            return R.ok().put("msg", "删除成功");
        }catch(Exception e){
            return R.err().put("msg", "删除失败");
        }
    }

    /***
     * 创建文件夹
     * @param bucketName
     * @return
     */
    @GetMapping("/files/makebecket")
    public R makeBecket(String bucketName){
        try{

            MinioUtils minio = MinioUtils.getMinio();
            minio.makeBecket(bucketName);
            return R.ok().put("msg", "创建成功");
        }catch(Exception e){
            return R.ok().put("msg", "创建失败");
        }
    }

    /***
     * 获取文件的url
     * @param bucketName
     * @param objectName
     * @return
     */
    @GetMapping("/files/geturl")
    public R getUrl(String bucketName,String objectName){
        try{

            MinioUtils minio = MinioUtils.getMinio();
            String objectUrl = minio.getObjectUrl(bucketName, objectName);
            return R.ok().put("data", objectUrl);
        }catch(Exception e){

            return R.err().put("data", "获取失败");
        }

    }

    @GetMapping("/files/objectrename")
    public R objectRename(String bucketName,String objectName,String reName){
        try{

            if(bucketName==null || objectName==null || reName==null){
                throw new RuntimeException("参数非法");
            }
            MinioUtils minio = MinioUtils.getMinio();
//        重命名 就是先复制再删除
//            1.复制文件
            minio.copyObject(bucketName, objectName, reName);
//            2.删除文件
            minio.deleteObjetc(bucketName,objectName);

            return R.ok().put("msg", "重命名成功");
        }catch(Exception e){

            return R.err().put("msg","重命名失败");

        }
    }



}
